package com.lixin.smartclassroom.ui.home.tab1.activity

import android.annotation.SuppressLint
import androidx.fragment.app.Fragment
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.api.getmyclassdetail
import com.lixin.smartclassroom.databinding.ActivityClassDetailBinding
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.home.adapter.PagerAdapter
import com.lixin.smartclassroom.ui.home.tab1.fragment.ClassDetailChildFra
import com.lixin.smartclassroom.ui.home.tab5.activity.EditClassAct
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.addIntArgument
import com.lixinkeji.extension.addStringArgument
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.load
import com.lixinkeji.extension.loadCircle
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.preview
import com.lixinkeji.extension.start
import com.lixinkeji.extension.visible

/**
 * Description 班级详情
 * Author: lwc
 * Date:2023/12/9 14:59
 */
class ClassDetailAct : BaseActivity<ActivityClassDetailBinding>() {
    override fun getViewBinding() = ActivityClassDetailBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    override fun title() = "班级详情"
    private lateinit var classid: String
    private lateinit var fwcode: String

    private var qrCodeUrl = ""

    @SuppressLint("SetTextI18n")
    override fun iniView() {
        classid = getIntentString("classid")
        getClassData()
        initSlidingLayout()


        receiveEvent<String>("refreshClassData") {
            getClassData()
        }
    }

    private fun getClassData() {
        scopeNetLife {
            val baseBean = getmyclassdetail(classid)
            if (baseBean.result == "0") {
                binding.apply {
                    tvClassCode.text = "访问码：" + baseBean.fwcode
                    tvClassName.text = baseBean.title
                    tvTeacherName.text = baseBean.createname
                    if (baseBean.createimage != null)
                        imgTeacher.loadCircle(baseBean.createimage)
                    if (!baseBean.imagecode.isNullOrEmpty()) {
                        ivQrcode.load(baseBean.imagecode!!)
                        qrCodeUrl = baseBean.imagecode
                    }
                    imgClass.load(baseBean.image!!)
                }
            }
        }
    }


    override fun onClick() {
        binding.apply {
            btnEdit.onClick {
                start(EditClassAct::class.java) {
                    putExtra("classid", classid)
                }
            }
            ivQrcode.onClick {
                val listUrl = ArrayList<String>()
                listUrl.add(qrCodeUrl)
                ivQrcode.preview(this@ClassDetailAct, 0, listUrl)
            }
        }
    }

    private fun initSlidingLayout() {
        val titles = ArrayList<String>()
        val fragments = ArrayList<Fragment>()

        when (user_role) {
            1 -> {
                titles.add("智慧任务")
                titles.add("场馆任务")
                titles.add("班级通知")
                fragments.add(
                    ClassDetailChildFra().addIntArgument("type", 1)
                        .addStringArgument("classid", classid).addIntArgument("fromAct", 2)
                )
                fragments.add(
                    ClassDetailChildFra().addIntArgument("type", 2)
                        .addStringArgument("classid", classid).addIntArgument("fromAct", 2)
                )
                fragments.add(
                    ClassDetailChildFra().addIntArgument("type", 4)
                        .addStringArgument("classid", classid)
                )
            }

            2 -> {
                titles.add("智慧任务")
                titles.add("场馆任务")
                titles.add("班级学生")
                titles.add("班级通知")
                titles.add("班级老师")
                fragments.add(
                    ClassDetailChildFra().addIntArgument("type", 1)
                        .addStringArgument("classid", classid).addIntArgument("fromAct", 2)
                )
                fragments.add(
                    ClassDetailChildFra().addIntArgument("type", 2)
                        .addStringArgument("classid", classid).addIntArgument("fromAct", 2)
                )
                fragments.add(
                    ClassDetailChildFra().addIntArgument("type", 3)
                        .addStringArgument("classid", classid)
                )
                fragments.add(
                    ClassDetailChildFra().addIntArgument("type", 4)
                        .addStringArgument("classid", classid)
                )
                fragments.add(
                    ClassDetailChildFra().addIntArgument("type", 5)
                        .addStringArgument("classid", classid)
                )
            }

            3 -> {
                titles.add("班级通知")
                fragments.add(
                    ClassDetailChildFra().addIntArgument("type", 4)
                        .addStringArgument("classid", classid)
                )
                binding.btnEdit.visible()
            }
        }
        val mPageAdapter = PagerAdapter(supportFragmentManager, fragments, titles)

        binding.apply {
            viewPager.adapter = mPageAdapter
            tabLayout.setViewPager(viewPager)
            tabLayout.onPageSelected(0)
        }


    }

}